const chunkify = function* (itr, size) {
  if (!Array.isArray(itr) || itr.length == 0) {
    return [];
  }
  let chunk = [];
  for (const v of itr) {
    chunk.push(v);
    if (chunk.length === size) {
      yield chunk;
      chunk = [];
    }
  }
  if (chunk.length) yield chunk;
};

// Examples
const x = [1, 2, 1, 3, 4, 1, 2, 5];
const res = [...chunkify(x, 2)];
console.log("chunkify :", res); // [ [ 1, 2 ], [ 1, 3 ], [ 4, 1 ], [ 2, 5 ] ]

function cf(arr, n) {
  if (!Array.isArray(arr) || arr.length == 0) {
    return [];
  }
  var tem = [],
    size = arr.length,
    count = Math.floor((size - 1) / n) + 1;

  while (count--) {
    tem.unshift(arr.slice(count * n, (count + 1) * n));
  }
  return tem;
}

// Examples
const res2 = cf(x, 2);
console.log("cf :", res2); // [ [ 1, 2 ], [ 1, 3 ], [ 4, 1 ], [ 2, 5 ] ]
